package cn.rui.service.impl;

import cn.rui.common.Result;
import cn.rui.mapper.LoginLogMapper;
import cn.rui.pojo.LoginLog;
import cn.rui.pojo.vo.PageResult;
import cn.rui.service.LoginLogService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 徽州大都督
 * @date 2021/05/14
 */
@Service
@Transactional
public class LoginLogServiceImpl implements LoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    @Override
    public void save(LoginLog loginLog) {
        loginLogMapper.insert (loginLog);
    }

    @Override
    public Map findPage(PageResult pageResult) {

        Page<LoginLog> page = new Page<> (pageResult.getCurrentPage (),pageResult.getPageSize ());
        QueryWrapper<LoginLog> wrapper = new QueryWrapper<> ();
        wrapper.orderByDesc ("get_date");
        IPage<LoginLog> loginLogIPage = loginLogMapper.selectPage (page, wrapper);
        Map map = new HashMap();
        map.put ("flag",true);
        map.put ("data",loginLogIPage.getRecords ());
        map.put ("total",loginLogIPage.getTotal ());
        return map;
    }

    @Override
    public Result deleteById(Integer id) {
        loginLogMapper.deleteById (id);
        return Result.getSuccess ("删除成功!");
    }


    public Result findAll() {
        try {
            List<LoginLog> loginLogs = loginLogMapper.selectList (null);

            return Result.getSuccess ("查询成功",loginLogs);
        } catch (Exception e) {
            return Result.getFailure ("操作失败");
        }
    }
}
